package com.expense.app.admin.services.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.logs.*;
import com.expense.app.admin.model.params.logs.*;
import com.expense.app.admin.services.LogsServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.LogsDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.Logs;
import com.expense.common.model.auth.AdminUser;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 导出日志
 */
@Service
public class LogsServImpl extends BaseService implements LogsServ {
    private static final Log log = LogFactory.get();

    @Autowired
    LogsDao logsDao;

    @Override
    public LogsListDTO findList(LogsListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("name",param.getName());
        condition.put("contract",param.getContract());
        condition.put("exportTime",param.getExportTime());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        logsDao.findListPage(pageQuery);
        return new LogsListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public Logs findOne(Long id) {
        return logsDao.findByIdAndDeleted(id);
    }

    @Override
    public LogsDTO findDetail(Long id) {
        Logs entity = logsDao.findByIdAndDeleted(id);
        return new LogsDTO(entity);
    }

    @Override
    public Logs saveOne(LogsParam param) {
        AdminUser adminUser = getCurrentUser();
        Logs entity = param.getEntity();
        entity.setUserName(adminUser.getName());
        entity.setUserId(adminUser.getId());
        entity.setExportTime(new Date());
        setBaseInfo(entity);
        logsDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        Logs entity = logsDao.findByIdAndDeleted(id);
        if(entity == null) throw new PlatformException("导出日志已不存在");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        logsDao.updateTemplateById(entity);
        //其他关联处理
        return id;
    }
}
